package com.object3D
{
	import away3d.Away3D;
	import away3d.containers.ObjectContainer3D;
	import away3d.entities.Mesh;
	import away3d.materials.ColorMaterial;
	import away3d.materials.MaterialBase;
	import away3d.materials.TextureMaterial;
	import away3d.materials.methods.EffectMethodBase;
	import away3d.materials.methods.FogMethod;
	import away3d.materials.methods.PlanarReflectionMethod;
	import away3d.primitives.PlaneGeometry;
	import away3d.textures.PlanarReflectionTexture;
	import away3d.utils.Cast;
	
	import com.engine.AwayEngine;
	import com.engine.PowerEngine;
	import com.manager.CarMaterial;
	import com.utils.PowerLoader;
	import com.utils.ZipResolve;
	import com.vo.CarVo;
	import com.vo.PartVo;
	
	import flash.display.Bitmap;
	
	public class NewBaseCar extends BaseCar
	{
//		protected static const FIX_TARGET_Y:int = -10;
		
//		protected var car3D:ObjectContainer3D;//3D视图容器
//		protected var carVo:CarVo;
//		
//		protected var shadowMaterial:TextureMaterial;
//		
//		protected var shadow:Mesh;
//		protected var wheelArray:Vector.<NewBaseWheel> = new Vector.<NewBaseWheel>();
		
//		public function show(carVo:CarVo,x:Number = 0,y:Number = 0,z:Number = 0):void{
//			this.carVo = carVo;
//			initFace();
//			car3D.x = x;
//			car3D.y = y;
//			car3D.z = z;
//		}
		
//		public function hide():void{
//			if(car3D != null)AwayEngine.removeMesh(car3D);
//		}
//		
//		public function replaceWheel(pvo:PartVo):void
//		{
//			var length:int = wheelArray.length - 1;
//			if(length < 0) return;
//			for(var i:int = length;i >= 0;i--)
//			{
//				(wheelArray[i] as NewBaseWheel).replaceWheel(pvo);
//			}
//		}
		
		protected override function open():void{
			var wheel:NewBaseWheel;
			wheel = new NewBaseWheel();
			wheel.show(car3D,0,FIX_TARGET_Y,0);
			wheelArray.push(wheel);
			wheel = new NewBaseWheel();
			wheel.show(car3D,0,FIX_TARGET_Y, 0,true);
			wheelArray.push(wheel);
			wheel = new NewBaseWheel();
			wheel.show(car3D,0,FIX_TARGET_Y + 0, 0);
			wheelArray.push(wheel);
			wheel = new NewBaseWheel();
			wheel.show(car3D,0,FIX_TARGET_Y + 0, 0,true);
			wheelArray.push(wheel);
		}
		
		protected override function initFace():void
		{
			if(car3D == null){
				car3D = new ObjectContainer3D();
				car3D.scale(.8);
				if(shadow == null)open();
				init();
			}
			AwayEngine.addMesh(car3D);
//			if(carVo.bumper != null)CarMaterial.bumperMaterial.color = carVo.bumper.textureColor;
			if(carVo.body != null){
				CarMaterial.bumperMaterial.color = CarMaterial.bodyMaterial.color = carVo.body.textureColor;
			}
		}
		
//		protected function init():void
//		{
//			initMaterial();
//			loadPNG();
//			loadModel();
//		}
		
//		protected function initMaterial():void
//		{
//			shadowMaterial = new TextureMaterial();
//			if(PowerEngine.platform != PowerEngine.PLAT_PHONE){
//				shadowMaterial.alphaBlending = true;
//				shadowMaterial.alpha = .5;
//				shadow = new Mesh(new PlaneGeometry(1120, 1120), shadowMaterial);
//				car3D.addChild(shadow);
//				shadow.y = FIX_TARGET_Y - 35;
//			}
//		}
		/**
		 * 加载所有贴图文件
		 */		
		protected override function loadPNG():void
		{
			PowerLoader.loadFile("models/audi/s3/CarShadow.png",loadMap,shadowMaterial);
		}		
		
		protected override function loadMap(bmp:Bitmap,material:TextureMaterial):void{
			material.texture = Cast.bitmapTexture(bmp);
		}
		
		protected override function loadModel():void
		{
			if(carVo.body != null)PowerLoader.loadByte(carVo.body.meshPath,loadZip,carVo.body.meshPath);
			if(carVo.bumper != null)PowerLoader.loadByte(carVo.bumper.meshPath,loadZip,carVo.bumper.meshPath);
			if(carVo.interior != null)PowerLoader.loadByte(carVo.interior.meshPath,loadZip,carVo.interior.meshPath);
			if(carVo.glass != null)PowerLoader.loadByte(carVo.glass.meshPath,loadZip,carVo.glass.meshPath);
			if(carVo.shell != null)PowerLoader.loadByte(carVo.shell.meshPath,loadZip,carVo.shell.meshPath);
			if(carVo.wheel != null)PowerLoader.loadByte(carVo.wheel.meshPath,loadZip,carVo.wheel.meshPath);
		}		
		
		protected override function loadMesh(mesh:Mesh, name:Object=null):void
		{
			var meshName:String = mesh.name;
			mesh.scale(200);
			car3D.addChild(mesh);
			if(meshName == 'L')
			{
				setMaterial(mesh,CarMaterial.bodyMaterial);
			}
			else if(meshName == 'G')
			{
				setMaterial(mesh,CarMaterial.glassMaterial);
			}
			else if(meshName == 'U')
			{
				setMaterial(mesh,CarMaterial.bumperMaterial);
			}
			else if(meshName == 'X')
			{
				setMaterial(mesh,CarMaterial.bumperMaterial);
			}
		}
		
		protected function loadZip(zipByteArray:Object,path:String):void
		{
			ZipResolve.loadByteArray(zipByteArray,loadMesh,path);
		}	
		
		protected function setMaterial(mesh:Mesh,material:MaterialBase):void
		{
			for(var i:int = mesh.numChildren - 1;i >= 0;i--)
			{
				((mesh.getChildAt(i)) as Mesh).material = material;
			}
		}
		
	}
}